<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <link rel="stylesheet" href="./css/bootstrap.min.css" />
    <link rel="stylesheet" href="./css/login.css" />
  </head>

  <body>
    <div class="login-box">
      <form>
        <div class="form-group mb-3">
          <label for="username">用户名</label>
          <!-- 账号 -->
          <input
            type="text"
            class="form-control"
            name="username"
            id="username"
            autocomplete="off"
          />
          <small id="emailHelp" class="form-text text-muted">用户名长度2~15位</small>
        </div>
        <div class="form-group mb-3">
          <!-- 密码 -->
          <label for="password">密码</label>
          <input type="password" class="form-control" name="password" id="password" />
          <small id="emailHelp" class="form-text text-muted">密码长度6~15位</small>
        </div>
        <button type="submit" class="btn btn-primary mb-3" id="btnLogin">登录</button>
        <div class="form-group mb-3">
          <a href="./register.html" style="color: #6c757d">还没有账号，去注册</a>
        </div>
      </form>
    </div>

    <script src="./lib/axios.js"></script>
    <script src="./lib/form-value.js"></script>
    <script src="./lib/request.js"></script>
    <script>
      document.querySelector('#btnLogin').addEventListener('click', async function(e){
      e.preventDefault()
      let data = val(document.querySelector('form'))
       let {data:res } = await axios.post('/api/login',data)
       localStorage.setItem('token',res.token)
       if(res.status === 0){
        location.href ='./index.html'
       }
     })
    </script>
  </body>
</html>
